package com.example.ck.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.ck.dao.AuditprojectDao;
import com.example.ck.dao.ProjectDao;
import com.example.ck.entity.Auditproject;
import com.example.ck.entity.Newproject;
import com.example.ck.entity.Project;
import com.example.ck.service.AuditprojectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


/**
 * 专家审核表(Expertaudit)表服务实现类
 *
 * @author makejava
 * @since 2024-03-25 12:51:32
 */
@Service
public class AuditprojectServiceImpl implements AuditprojectService {
    @Autowired
    private AuditprojectDao auditprojectDao;
    @Autowired
    private ProjectDao projectDao;

    @Override
    public boolean addAuditProject(Auditproject auditproject) {
        String expertNames = auditproject.getExpertName();
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", auditproject.getProjectId());

        Project project = projectDao.selectOne(qw);
        // 根据 expertNames 遍历出每一个专家
        String[] expertNameArr = expertNames.split(",");
        for (String expertName : expertNameArr) {
            Auditproject auditproject1 = new Auditproject();
            auditproject1.setProjectId(auditproject.getProjectId());
//            auditproject1.setProject(project);
            auditproject1.setExpertName(expertName);
            auditproject1.setUserPhone(auditproject.getUserPhone());
            int i = auditprojectDao.insert(auditproject1);
        }
        return true;
    }

    /**
     * 根据专家id查询待审核项目信息
     *
     * @param expertName
     * @return
     */
    @Override
    public List<Auditproject> getAuditProjectByUserId(String expertName) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("expert_name", expertName);
        // 查询出所有当前专家的待审核项目
        List<Auditproject> list = auditprojectDao.selectList(qw);
        for (Auditproject auditproject : list) {
            Project project = projectDao.selectById(auditproject.getProjectId());
            auditproject.setProject(project);
        }
        return list;
    }

    /**
     * 专家根据项目id来设置项目状态
     */
    @Override
    public boolean setProjectStatus(int projectId) {
        QueryWrapper qw = new QueryWrapper<>();
        qw.eq("id", projectId);
        Project project = projectDao.selectOne(qw);
        project.setAuditorNum(project.getAuditorNum() - 1);
        if (project.getAuditorNum() != 0)
            return projectDao.update(project, qw) > 0;
        return false;
    }
}
